package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.excel.listener.ExportListener;
import com.woniu.supermarket.mapper.SalesMapper;
import com.woniu.supermarket.service.IDealService;
import com.woniu.supermarket.vo.dealAdvancedSearchVo;
import com.woniu.supermarket.vo.dealSummaryVo;
import com.woniu.supermarket.vo.dealVo;
import com.woniu.supermarket.vo.exportDealSummaryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DealServiceImpl implements IDealService {

    private List<exportDealSummaryVo> ls ;

    @Autowired
    SalesMapper salesMapper;

    @Override
    public PageInfo<dealVo> findByCons(Integer now, Integer size, String salecodeOrName) {
        PageHelper.startPage(now, size);
        List<dealVo> lists = salesMapper.selectDealByCons(salecodeOrName);
        for (dealVo d : lists) {
            String code = d.getBussinessCode().substring(0, 4);
            if (code.equals("XSTH")) {
                d.setTypeNum(1);
            } else if (code.equals("XSXS")) {
                d.setTypeNum(0);
            }
        }
        return new PageInfo<>(lists);
    }

    @Override
    public PageInfo<dealVo> advancedsearchByCons(Integer now, Integer size, Date dealStartTime, Date dealEndTime, String memberTel, String salecodeOrName, Integer shid, String staff, Integer typeOrder) {
        PageHelper.startPage(now, size);

        List<dealVo> lists = salesMapper.selectAdvancedDealByCons(dealStartTime, dealEndTime, memberTel, salecodeOrName, shid, staff, typeOrder);
        for (dealVo d : lists) {
            String code = d.getBussinessCode().substring(0, 4);
            if (code.equals("XSTH")) {
                d.setTypeNum(1);
            } else if (code.equals("XSXS")) {
                d.setTypeNum(0);
            }
        }

        return new PageInfo<>(lists);
    }

    @Override
    public PageInfo<dealSummaryVo> querySummaryDeal(Integer now, Integer size, String salecodeOrName, Integer searchType) {
        PageHelper.startPage(now, size);
        List<dealSummaryVo> lists = salesMapper.selectDealSummaryByCons(salecodeOrName,searchType);
        for(dealSummaryVo ds : lists){
            System.out.println(ds);
            if(searchType == 0){
                ds.setDateTime(ds.getYear()+"-"+ds.getMonth()+"-"+ds.getDay());
            }else if(searchType == 1){
                ds.setDateTime(ds.getYear()+"-"+ds.getMonth());
            }else{
                ds.setDateTime(ds.getYear()+"");
            }
        }


        return new PageInfo<>(lists);
    }

    @Override
    public void getlist(List<exportDealSummaryVo> dealSummaryList) {
        ls = dealSummaryList;
    }

    @Override
    public void exportExcel(HttpServletResponse response) throws IOException{
        long beforeTime = System.currentTimeMillis();

        new ExportListener<>().
                exportExcel(response, "员工信息", exportDealSummaryVo.class, ls);

        long afterTime = System.currentTimeMillis();
        log.info("耗时:{}", afterTime - beforeTime);
    }
}


